package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessInformation;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.FarmingInfoListVO;
import com.digitalchina.lxnsy.entity.FarmingInfoTypeVO;
import com.digitalchina.lxnsy.entity.TechnologiesVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.FarmingInfoHolder;
import com.digitalchina.lxnsy.viewholder.FarmingInfoTypeHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/8/6 14:24
 * 修改备注：
 * ================================================
 */
public class FarmingInformationActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.recycler_tab)
    EasyRecyclerView recyclerTab;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;


    private RecyclerArrayAdapter adapter;
    private RecyclerArrayAdapter adapterTab;
    private int page = 1;
    private int limit = 10;

    private int msgWhat = -1;
    private String typeId = "";


    private List<FarmingInfoListVO> voList = new ArrayList<>();
    private List<FarmingInfoTypeVO> typeList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_farming_information);
        setTitle("农业资讯");
    }

    @Override
    public void initMember() {

        //顶部tab
        recyclerTab.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerTab.setAdapter(adapterTab = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new FarmingInfoTypeHolder(parent);
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 0, 0));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new FarmingInfoHolder(parent);
            }
        });
        BusinessInformation.getInformationList(activity,mHandler);
        onRefresh();

    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });


        adapterTab.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                for (int i = 0; i < typeList.size(); i++) {
                    typeList.get(i).setChecked(i == position);
                }
                adapterTab.notifyDataSetChanged();

                FarmingInfoTypeVO vo = (FarmingInfoTypeVO) adapterTab.getAllData().get(position);
                typeId = vo.getId();
                onRefresh();
            }
        });


        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                GotoUtil.gotoActivity(FarmingInformationActivity.this, FarmingInfoDetailActivity.class, "FarmingInfoListVO", adapter.getAllData().get(position));
            }
        });
    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                msgWhat = msg.what;

                switch (msg.what) {
                    case MSG.MSG_GET_INFOMATION_LIST_SUCCESS:
                    case MSG.MSG_GET_INFOMATION_LIST_BY_TYPE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_INFOMATION_LIST_FIELD:
                    case MSG.MSG_GET_INFOMATION_LIST_BY_TYPE_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };

    }

    // 设置顶部tab类型
    private void setType() {
        if (typeList == null)
            return;

        FarmingInfoTypeVO vo = new FarmingInfoTypeVO();
        vo.setTitle("全部");
        vo.setId("");
        typeList.add(0, vo);

        for (int i = 0; i < typeList.size(); i++) {
            typeList.get(i).setChecked(i == 0);
        }

        adapterTab.clear();
        adapterTab.addAll(typeList);

    }

    @Override
    public void callBack(Object object) {

        switch (msgWhat) {
            case MSG.MSG_GET_INFOMATION_LIST_SUCCESS:
                typeList = FastJsonUtil.getListBeanRow(object.toString(), "rows", FarmingInfoTypeVO.class);
                setType();
                break;
            case MSG.MSG_GET_INFOMATION_LIST_BY_TYPE_SUCCESS:
                voList = FastJsonUtil.getListBeanRow(object.toString(), "rows", FarmingInfoListVO.class);
                break;
        }

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessInformation.getInformationListByType(this, typeId, page, limit,"1", mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessInformation.getInformationListByType(this, typeId, page, limit, "1",mHandler);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
